package com.cheung.leetcode.stack;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.stack
 * @Author :  cheungming
 * @CreateTime : 2024-06-18 10:15:49
 * @Description : 每日 温度
 * @Version : 1.0
 */
public class Code739Test {

    public int[] dailyTemperatures(int[] temperatures) {
        int length = temperatures.length;
        int[] result = new int[length];

        // 栈中存放元素的下标
        Deque<Integer> stack = new LinkedList<>();
        stack.push(0);

        for (int i = 1; i < length; i++) {
            while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                // 当前元素大于栈顶元素, 则当前元素出栈, 并且记录当前元素与栈顶元素的距离
                result[stack.peek()] = i - stack.pop();
            }
            stack.push(i);
        }
        return result;
    }

    @Test
    public void test1() {
        int[] temperatures = {73, 74, 75, 71, 69, 72, 76, 73};
        int[] result = {1, 1, 4, 2, 1, 1, 0, 0};
        assert Arrays.equals(dailyTemperatures(temperatures), result);
    }

    @Test
    public void test2() {
        int[] temperatures = {30, 40, 50, 60};
        int[] result = {1, 1, 1, 0};
        assert Arrays.equals(dailyTemperatures(temperatures), result);
    }

    @Test
    public void test3() {
        int[] temperatures = {30, 60, 90};
        int[] result = {1, 1, 0};
        assert Arrays.equals(dailyTemperatures(temperatures), result);
    }
}
